package com.naiterui.ehp.bs.patient.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.patient.config.SubscribeConfig;
import com.naiterui.ehp.bs.patient.config.SubscribeProperties;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IDrStarInfoService;
import com.naiterui.ehp.bs.patient.service.IPatientDoctorService;
import com.naiterui.ehp.bs.patient.service.IPatientService;
import com.naiterui.ehp.bs.patient.vo.SubscribeTemplateListVO;
import com.naiterui.ehp.bs.patient.vo.doctor.StarInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.USER_INFO})
@Controller
@RequestMapping("ap")
@RequiredArgsConstructor
public class ApIndexController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApIndexController.class);

    private final IDrStarInfoService drStarInfoService;
    private final IPatientService patientService;
    private final IPatientDoctorService patientDoctorService;
    private final SubscribeConfig subscribeConfig;

    /**
     * 首页数据
     * IndexController.index()
     *
     * @param patientId
     *
     * @return
     *
     * @Author YongYang
     * @Date 2016年7月21日
     * @since 1.0.0
     */
    @ResponseBody
    @RequestMapping(value = "/index")
    @ApiOperation(value = "患者首页", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> index(Long patientId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("-----------IndexController Method Index PatientId：{}", patientId);
        if (patientId == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        Map<String, Object> data = new HashMap<>();
        List<StarInfoVO> starInfoVos = this.drStarInfoService.findDrStarInfo(1);
        data.put("infos", starInfoVos);
        List<BigInteger> num = this.patientService.getPersonalIds(patientId);
        Long firstDoctorId = this.patientDoctorService.getFirstDoctorByPatient(patientId);
        data.put("firstDoctorId", firstDoctorId);
        if (num == null || num.isEmpty()) {
            data.put("personal", 0);
        } else {
            data.put("personal", 1);
        }

        // 医院客服电话
        data.put("csPhone", ConfigUtil.getString("customerServPhone"));
        return data;
    }

    @GetMapping("/subscribe/templates")
    @ApiOperation("获取订阅通知模板列表")
    @ApiImplicitParam(name = "type", value = "类型：1.问诊 2.购药 3.续方", required = true, dataType = "int", paramType = "query")
    public List<SubscribeTemplateListVO> getSubscribeTemplateList(Integer type) throws BusinessException {
        if (type == null) {
            LOGGER.info("参数校验失败");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        List<SubscribeProperties> list = subscribeConfig.getTemplate();
        return list.stream().map(s -> {
            SubscribeTemplateListVO templateListVO = null;
            // 问诊
            if (SubscribeTemplateListVO.TYPE_CONSULT == type) {
                if (SubscribeTemplateListVO.LABEL_MEDICAL.equals(s.getLabel())
                        || SubscribeTemplateListVO.LABEL_REMINDER.equals(s.getLabel())
                        || SubscribeTemplateListVO.LABEL_CONSULT_REPLY.equals(s.getLabel())) {
                    templateListVO = SubscribeTemplateListVO.builder().templateId(s.getTemplateId()).title(s.getTitle()).build();
                }
            }
            // 购药
            else if (SubscribeTemplateListVO.TYPE_BUYING_MEDICINE == type) {
                if (SubscribeTemplateListVO.LABEL_PLAN.equals(s.getLabel())) {
                    templateListVO = SubscribeTemplateListVO.builder().templateId(s.getTemplateId()).title(s.getTitle()).build();
                }
            }
            // 续方
            else if (SubscribeTemplateListVO.TYPE_CONTINUATION == type) {
                if (SubscribeTemplateListVO.LABEL_APPLY_RESULT.equals(s.getLabel())) {
                    templateListVO = SubscribeTemplateListVO.builder().templateId(s.getTemplateId()).title(s.getTitle()).build();
                }
            }
            return templateListVO;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }
}
